using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using OpenTK.Math;
using Dogfight2008.Common;

namespace Dogfight2008
{
  public class PressFireState : IState
  {
    //RotatedBody2d demoPlane = new RotatedBody2d();
    //double rotation = 0;
    //bool left = false;
    //bool right = false;
    double time = 0;
    //Shot shot = new Shot(Calc.v(0, 0), 0, 0);
    //const Keys plane1Left = Keys.A;
    //const Keys plane1Right = Keys.D;
    //const Keys plane1Shoot = Keys.ShiftKey;

    public DFState KeyDown(Keys key)
    {
      switch (key)
      {
        case Keys.Escape:
          return DFState.PressFire;
        //case plane1Left:
        //  left = true;
        //  right = false;
        //  return DFState.PressFire;
        //case plane1Right:
        //  right = true;
        //  left = false;
        //  return DFState.PressFire;
        //case plane1Shoot:
        //  shot = new Shot(demoPlane.Position, rotation, 200);
        //  return DFState.PressFire;
        default:
          return DFState.InGame;
      }
    }

    //Vector2 GetVel(double rot)
    //{
    //  return 3.0f * new Vector2((float)(Math.Cos(rot) * 10.0 / 1000.0), (float)(Math.Sin(rot) * 10.0 / 1000.0));
    //}

    public PressFireState()
    {
      //demoPlane.Position = Q.v(0, 0);
      //demoPlane.Velocity = GetVel(0);
      //demoPlane.Angle = 0;
    }

    //static double maxx = 75;
    public DFState Tick(double deltaMS)
    {
      time += deltaMS;

      //shot.Tick(deltaMS);

      //demoPlane.Tick(deltaMS);
      //if (demoPlane.Position.X > maxx)
      //  demoPlane.Position.X -= (float)(maxx * 2);
      //if (demoPlane.Position.X < -maxx)
      //  demoPlane.Position.X += (float)(maxx * 2);

      //if (left && !right)
      //{
      //  rotation = (rotation + 0.003 * deltaMS) % (Math.PI * 2);
      //  demoPlane.Angle = rotation;
      //  demoPlane.Velocity = GetVel(rotation);
      //}
      //if (!left && right)
      //{
      //  rotation = (rotation + Math.PI * 2 - 0.003*deltaMS) % (Math.PI * 2);
      //  demoPlane.Angle = rotation;
      //  demoPlane.Velocity = GetVel(rotation);
      //}

      return DFState.PressFire;
    }

    public DFState KeyUp(Keys key)
    {
      //if (key == plane1Left)
      //  left = false;
      //if (key == plane1Right)
      //  right = false;
      return DFState.PressFire;
    }

    public void Render(IRenderer renderer)
    {
      //RenderDemoPlane(renderer);
      if (ShowFlashText())
        StateUtil.RenderNiceCenterText(renderer, "[Press Fire]");
      //RenderShot(renderer);
      RenderDogfight2008Title(renderer);
    }

    //void RenderShot(IRenderer renderer)
    //{
    //  Box2d box = shot.GetBox2d();
    //  renderer.Color(Color.Black);
    //  RenderBox(renderer, box);
    //}

    bool ShowFlashText()
    {
      return ((int)((time / 1000) * 4)) % 2 == 0;
    }

    void RenderDogfight2008Title(IRenderer renderer)
    {
      StateUtil.RenderNiceText(renderer, "DOGFIGHT", 0.5, 0.1, 0.5, Color.White, Color.Black);
    }

    //void RenderDemoPlane(IRenderer renderer)
    //{
    //  Vector3 pos3d = new Vector3(demoPlane.Position.X, 0, demoPlane.Position.Y);
    //  renderer.PushMatrix();
    //  renderer.Translate(pos3d);
    //  renderer.Rotate(-Vector3.UnitY, rotation);
    //  renderer.Rotate(Vector3.UnitZ, -Math.PI / 2);
    //  renderer.Color(Color.Green);
    //  float mult = 1.0f;
    //  float amin = mult * -3.6f, amax = mult * 1.5f;
    //  float bmin = mult * -0.25f, bmax = mult * 0.45f;
    //  //renderer.DrawLine(new Vector3(0, amin, bmin), new Vector3(0, amin, bmax));
    //  //renderer.DrawLine(new Vector3(0, amax, bmin), new Vector3(0, amax, bmax));
    //  //renderer.DrawLine(new Vector3(0, amin, bmin), new Vector3(0, amax, bmin));
    //  //renderer.DrawLine(new Vector3(0, amin, bmax), new Vector3(0, amax, bmax));
    //  renderer.DrawMesh(Mesh.GreenPlane);
    //  renderer.PopMatrix();

    //  Box2d box1 = new Box2d(0, 0, -10, 10, -20, 20, 0);
    //  Box2d box2 = new Box2d(demoPlane.Position.X, demoPlane.Position.Y, amin, amax, bmin, bmax, rotation);
    //  if (box1.Intersects(box2))
    //    renderer.Color(Color.Red);
    //  else
    //    renderer.Color(Color.Green);
    //  RenderBox(renderer, box2);
    //  RenderBox(renderer, box1);

    //  //Box2d box = new Box2d(demoPlane.Position.X, demoPlane.Position.Y,
    //  //  -20, 5, -2, 2, rotation);
    //}

    //Vector3 Logic2World(double x, double y)
    //{
    //  return new Vector3((float)x, 0, (float)y);
    //}

    //void RenderBox(IRenderer renderer, Box2d box)
    //{
    //  renderer.PushMatrix();

    //  renderer.Translate(Logic2World(box.X, box.Y));
    //  renderer.Rotate(-Vector3.UnitY, box.Rotation);

    //  float amin = (float)box.Amin;
    //  float amax = (float)box.Amax;
    //  float bmin = (float)box.Bmin;
    //  float bmax = (float)box.Bmax;

    //  renderer.DrawLine(new Vector3(amin, 0, bmin), new Vector3(amin, 0, bmax));
    //  renderer.DrawLine(new Vector3(amax, 0, bmin), new Vector3(amax, 0, bmax));
    //  renderer.DrawLine(new Vector3(amin, 0, bmin), new Vector3(amax, 0, bmin));
    //  renderer.DrawLine(new Vector3(amin, 0, bmax), new Vector3(amax, 0, bmax));

    //  renderer.PopMatrix();
    //}
  }
}
